package game;

import java.awt.Point;
import java.util.*;

import org.w3c.dom.Element;

/**
 * <p>This class represents a {@code Bishop} from a game of chess.</p>
 * 
 * @author Nicole Waldrum
 * @date January 30, 2010
 * @version 1.0
 * 
 * @author Mark Kelly
 * @date February 19, 2010
 * @version 2.0
 * 
 * @author Trevor Bastian
 * @date February 20, 2010
 * @version 2.0
 */
public class Bishop extends Piece {
	// add this Piece subclass to the list of Piece types
	static {
		Piece.addPieceType(Bishop.class);
	}
	
	private static final String DEFAULT_LETTER = "B";
	/**
	 * <p>Creates a new instance of {@code Bishop}</p>
	 * 
	 * @param name the name of this {@code Bishop}
	 * @param position the initial position of this {@code Bishop}
	 * @param team the {@code Team} that this {@code Bishop} is on
	 */
	public Bishop(String name, Point position, Team team) {
		super(name, position, team);
	}
	
	/**
	 * Creates a Bishop from XML
	 * @param bishop
	 */
	public Bishop (Element bishop) {
		super(bishop);
	}
	
	/**
	 * <p>Creates a new instance of {@code Bishop}</p>
	 * 
	 * @param position the initial position of this {@code Bishop}
	 * @param team the {@code Team} that this {@code Bishop} is on
	 */
	public Bishop(Point position, Team team) {
		super(position, team);
	}

	/**
	 * {@inheritDoc}
	 */
	public String toLetter() {
		return DEFAULT_LETTER;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public List<Point> checkMovePattern(Point dest) {
		List<Point> ret = new ArrayList<Point>();
        Point p = this.getLocation();
        int dx = (int) ( dest . getX () - p . getX ());
        int dy = (int) ( dest . getY () - p . getY ());
        int i = ( Math . abs ( dx ) > Math . abs ( dy ) ) ? Math . abs ( dx ) : Math . abs ( dy );
        
        if ( dx != 0 )
            dx /= Math . abs ( dx );    // give dx a magnitude of 1
        if ( dy != 0 )
            dy /= Math . abs ( dy );    // give dy a magnitude of 1
        
        for ( int j = 0; j < i - 1; j++ ) {
            p = new Point ((((int) p . getX ()) + dx ), (((int) p . getY ()) + dy ));
            ret . add( p );
        }
        
        return ret;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean isValidMove ( Piece piece, Point dest ) {
		Point start = piece.getLocation();
		if ( Math . abs ( start . getX () - dest . getX ()) == Math . abs ( start . getY () - dest . getY ())) {
			return super.isValidMove(piece, dest);
		}
		return false;
	}
}
